<?php
namespace app\admin\controller;
use app\admin\model\{StoreCoupon,Goods,StoreCategory,StoreCouponIssue,StoreCouponIssueUser,StoreCouponUser};

class Coupon extends Base{
    

    /**
     * 首页
     */
    public function index(){
        if(IS_POST) {
            $search  = request()->param();
            $where = [];
            $where[] = ['is_del','=',0];
            if(isset($search["name"]) && $search["name"]!="") $where[] = ["name","=",$search["name"]];
            $db   = StoreCouponIssue::with('coupon')->where($where)->order('id desc');
            $list = $db->limit(paginator())->append(['type_name','receive_date','publish_num','full_name','status_name'])->select();
             if(count($list)){
                foreach($list as &$v){
                    
                }
            }
            admin_response(0, 'success', $list, $db->count());
        }
        return $this->render_table([
            "page"=>true,
            "toolbar_row" => [ 
                    table_button('admin/Coupon/coupon_log',"领取记录","frame",'',['800px','650px']),
                    table_button('admin/Coupon/delete',"删除","confirm",'确认删除么，请谨慎操作',['800px','650px'])
                    ],

            "cols" => [ 
                    ["type"=>"checkbox"],
                    ["field"=>"coupon_title", "title"=>"优惠券名称","align"=>"center"],
                    ["field"=>"type_name", "title"=>"优惠券类型","align"=>"center"],
                    ["field"=>"receive_date", "title"=>"领取日期","align"=>"center"],
                    ["field"=>"publish_num", "title"=>"发布数量","align"=>"center"],
                    ["field"=>"full_name", "title"=>"消费满赠","align"=>"center"],
                    ["field"=>"status_name", "title"=>"状态","align"=>"center"]

            ],


            ],false);

    }





        /**
     * 删除
     * @return void
     * @author Sam
     * date 2021-07-01
     */
    public function delete(){
        $id = param_check('id');
        if(!$StoreCouponIssue = StoreCouponIssue::get($id)) json_response(0,'不存在');
        $StoreCouponIssue->is_del = 1;
        if($StoreCouponIssue->save()) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }

    /**
     * 领取记录
     * @return [type] [description]
     */
    public function coupon_log(){
        $id = param_check('id');
        if(IS_POST) {
            $search  = request()->param();
            $where = [];
            $where[] = ['issue_coupon_id','=',$id];
            if(isset($search["name"]) && $search["name"]!="") $where[] = ["name","=",$search["name"]];
            $db   = StoreCouponIssueUser::withJoin('user')->where($where)->order('add_time desc');
            $list = $db->limit(paginator())->select();
             if(count($list)){
                foreach($list as &$v){
                        $v["avatar"] = table_img($v["avatar"]);
                        $v['add_time'] = date('Y/m/d H:i',$v['add_time']);
                    }
                }

            admin_response(0, 'success', $list, $db->count());
        }
        if(IS_EXCEL) {
            $db = StoreCouponIssueUser::with('user')->where($where)->order('id asc');
            big_array_to_csv($db, '数据导出', []);
        }
        return $this->render_table([
            "page"=>true,
            "export" => true,
            "frame" => true,
            "cols" => [ 
                    ["type"=>"checkbox"],
                    ["field"=>"nickname", "title"=>"用户昵称","align"=>"center"],
                    ["field"=>"avatar", "title"=>"用户头像","align"=>"center"],
                    ["field"=>"add_time", "title"=>"领取时间","align"=>"center"]
            ],

            ],false);
    }

    /**
     * 领取详情
     * @return [type] [description]
     */
    public function coupon_user(){
        $search  = request()->param();
        if(IS_POST) {
            $db   = StoreCouponUser::search_model($search);
            $list = $db->limit(paginator())->select();
             if(count($list)){
                foreach($list as &$v){
                        $v["avatar"] = table_img($v["avatar"]);
                        $v['add_time'] = date('Y/m/d H:i',$v['add_time']);
                    }
                }

            admin_response(0, 'success', $list, $db->count());
        }
        if(IS_EXCEL) {
            $db = StoreCouponUser::with('user')->where($where)->order('id asc');
            big_array_to_csv($db, '数据导出', []);
        }
        return $this->render_table([
            "page"=>true,
            "export" => true,
            "cols" => [ 
                    ["type"=>"checkbox"],
                    ["field"=>"coupon_title", "title"=>"优惠券名称","align"=>"center"],
                    ["field"=>"coupon_price", "title"=>"优惠券面值","align"=>"center"],
                    ["field"=>"use_min_price", "title"=>"优惠券最低消费","align"=>"center"],
                    ["field"=>"add_time", "title"=>"优惠券开始时间","align"=>"center"],
                    ["field"=>"end_time", "title"=>"优惠券结束时间","align"=>"center"],
                    ["field"=>"status_name", "title"=>"状态","align"=>"center"]
            ],
            "where"=>[
                'title'  => ['type'=>'input', 'title'=>'优惠券搜索','placeholder'=>'优惠券搜索'],
                 'nickname'  => ['type'=>'input', 'title'=>'用户','placeholder'=>'用户搜索'],
                'status'  => ['type'=>'select', 'title'=>'状态','option'=>[
                    ['title'=>'未使用','name'=>0],
                    ['title'=>'已使用','name'=>1],
                    ['title'=>'已过期','name'=>2],
                ]],
            ]
            ],false);
    }

    /**
     * 审核通过
     * @return [type] [description]
     */
    public function verify(){
        $id = param_check('id');
        $exchange = Exchange::find($id);
        if($exchange->status > 0) json_response(0,'状态异常');
        $exchange->status =1;
        if($exchange->save()) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }

    /**
     * 审核拒绝
     * @return [type] [description]
     */
    public  function refund(){
        $id = param_check('id');
        $exchange = Exchange::find($id);
        if($exchange->status > 0) json_response(0,'状态异常');
        if(request()->isPost()) {
            $reason = param_check('reason');
            try{
                $exchange->status  = 2;
                $exchange->refund_reason = $reason;
                $exchange->save();
                Exchange::in_score($exchange->uid,$exchange->score,3,$id);
                Db::commit();
            }catch(\Exception $e){
                Db::rollback();
            }
            json_response(1,'操作成功');
         }else{
            return $this->render_form([
              
                ['type'=>'textarea', 'name'=>'reason', 'title'=>'拒绝理由', 'lay-verify'=>'required'],
                
            ]);
         }
    }
}